/*
 * BSM6.0
 *
 * Copyright 2012-2013 (C) SINODATA CO., LTD. All Rights Reserved.
 */
package com.sinodata.bsm.center.web.action.fault;

import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.sinodata.bsm.center.bean.EventTypeBean;
import com.sinodata.bsm.center.bean.ResTypeBean;
import com.sinodata.bsm.center.cache.EventTypeCache;
import com.sinodata.bsm.center.cache.ResCache;
import com.sinodata.bsm.center.cache.TrapRuleCache;
import com.sinodata.bsm.center.service.fault.TrapRuleService;
import com.sinodata.bsm.center.util.web.Struts2Utils;
import com.sinodata.bsm.center.web.action.BaseAction;
import com.sinodata.bsm.common.vo.EventType;
import com.sinodata.bsm.common.vo.FaultTrapRule;
import com.sinodata.bsm.common.vo.Res;

/**
 * <p>
 * Description: 
 * </p>
 *
 * @author wangyazhou
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-12-18 下午10:59:37          wangyazhou        1.0         To create
 * </p>
 *
 * @since 
 * @see     
 */
public class TrapRuleAction extends BaseAction<FaultTrapRule> {

    private static final Logger logger = Logger.getLogger(TrapRuleAction.class);

    private static final long serialVersionUID = 1L;

    @Autowired
    private TrapRuleCache trapRuleCache;

    @Autowired
    private TrapRuleService trapRuleService;

    @Autowired
    private ResCache resCache;

    @Autowired
    private EventTypeCache eventTypeCache;

    private FaultTrapRule entity;

    private Long id;

    public String add() {
        Res res = resCache.get(Long.valueOf(request.getParameter("resId")));
        ResTypeBean resTypeBean = ResTypeBean.get(res.getResTypeId());
        List<EventType> listEventType = resTypeBean.eventType();
        //自定义事件类别
        List<EventType> list = eventTypeCache.findByResId(res.getId());
        List<EventType> listResult = new ArrayList<EventType>();
        if (listEventType != null) {
            for (EventType type : listEventType) {
                if (type.getCatalog() == 20) {
                    listResult.add(type);
                }
            }
        }
        if (list != null) {
            for (EventType type : list) {
                if (type.getCatalog() == 20) {
                    listResult.add(type);
                }
            }
        }
        Struts2Utils.getRequest().setAttribute("eventTypeList", listResult);
        Struts2Utils.getRequest().setAttribute("resId", request.getParameter("resId"));
        return "add";
    }

    @Override
    public String list() throws Exception {
        Long resId = Long.valueOf(request.getParameter("resId"));
        JSONObject json = new JSONObject();
        List<FaultTrapRule> list = trapRuleCache.getAll();
        JSONArray jary = new JSONArray();
        for (FaultTrapRule rule : list) {
            if (!resId.equals(rule.getResId())) {
                continue;
            }
            JSONObject obj = new JSONObject();
            obj.element("op", "<div style='width: 60px;align:center;'>&nbsp;&nbsp;<a class='deletebutton' href='javascript:deleteRule(" + rule.getId() + ");'>删除</a></div>");
            obj.element("name", rule.getName());
            obj.element("eventlev", rule.getEventLevel());
            obj.element("duration", rule.getDuration());
            obj.element("eventtype", EventTypeBean.get(rule.getEventType()).eventType().getName());
            obj.element("securitylev", rule.getFiltLevel());
            obj.element("errorstatus", rule.getFiltStatus());
            obj.element("generic", rule.getFiltGeneric());
            obj.element("msg", rule.getFiltMsg());
            jary.add(obj);
        }
        json.element("iTotalRecords", jary.size());
        json.element("iTotalDisplayRecords", jary.size());
        json.element("aaData", jary);
        renderText(json.toString());
        return null;
    }

    @Override
    public String input() throws Exception {
        return null;
    }

    @Override
    public String save() throws Exception {
        trapRuleService.saveOrUpdate(entity);
        trapRuleCache.put(entity.getId(), entity);
        return null;
    }

    @Override
    public String delete() throws Exception {
        if (trapRuleCache.get(id) != null) {
            trapRuleService.delete(id);
            trapRuleCache.remove(id);
        }
        return null;
    }

    @Override
    protected void prepareModel() throws Exception {
        if (id != null) {
            entity = trapRuleCache.get(id);
            if (entity == null) {
                entity = new FaultTrapRule();
            }
        } else {
            entity = new FaultTrapRule();
        }
    }

    @Override
    public FaultTrapRule getModel() {
        return entity;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

}
